package com.huluobo.a2109amvvmdemo.livedata;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.widget.TextView;

import com.huluobo.a2109amvvmdemo.R;

import org.w3c.dom.Text;

public class StudentActivity extends AppCompatActivity {

    private StudentViewModel studentViewModel;

    private TextView tvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);

        tvMessage = findViewById(R.id.tvMessage);
        studentViewModel = new ViewModelProvider(this).get(StudentViewModel.class);

        //创建一个对象,给ViewModel的LiveData进行赋值
        Student student = new Student("LC", 1, 90);
        studentViewModel.setStudentMutableLiveData(student);


        //观察LiveData的值是否发生变化,如果发生变化,就给TextView赋值
        studentViewModel.getStudentMutableLiveData().observe(this, new Observer<Student>() {
            @Override
            public void onChanged(Student student) {
                tvMessage.setText("分数" + student.getScore());
            }
        });
    }
}